<?php

class ReviewController extends Zend_Controller_Action 
{
    protected $_model;
	protected $user;
	public function preDispatch(){
		if (Zend_Auth::getInstance()->hasIdentity()) {
			$this->user = Zend_Auth::getInstance()->getIdentity(); 
            $actionName = $this->getRequest()->getActionName();             
            if ($actionName == 'index' || 
                $actionName == 'assigned' ||
                $actionName == 'write') {
                if($this->user->isReviewer == 0){
                    $this->_helper->redirector('index', 'login');
                }                    
            }
		}else{
			$this->_helper->redirector('index', 'login');
		}
	}
	public function init()
    {
        $ajaxContext = $this->_helper->getHelper('AjaxContext');
        $ajaxContext->addActionContext('view', 'html')
                    ->addActionContext('comment', 'html')                    
                    ->initContext();
    }
    
    public function indexAction(){
    	$this->view->navTitle = $this->view->translate('write');
    }
	public function viewAction(){
	
	}
	public function assignedAction(){
   		$this->view->navTitle = $this->view->translate('assigned');
	}
	
	public function writeAction(){
		$request = $this->getRequest();
		if($request->isPost()){
			$data = $request->getParams();
			$data['isReviewed'] = true;
			$data['isSended'] = false;
			$review = $this->getReview();
			$review->save($data);
			die();
		}
	}
	public function getreviewAction()
    {
    	$id = $this->user->id;
        $model = $this->getPaper();
		$request = $this->getRequest();
    	$callback = $request->getParam('callback');
        $papers = $model->getPapersForReviewerId($id);
		$count = count($papers);
        $json = array();
        $json['totalCount'] = $count;
        $json['papers'] = $papers;
        echo $callback.'('.Zend_Json::encode($json).')';
        die();
    }
	public function getreviewsAction(){
		$model = $this->getReview();
		$request = $this->getRequest();
		$id = $request->getParam('paper_id');
    	$callback = $request->getParam('callback');
        $r = $model->getByPaperId($id);
        $json = array();
		$reviews = array();
		$reviews = $r;
        $json['totalCount'] = count($reviews);
        $json['reviews'] = $reviews;
        echo $callback.'('.Zend_Json::encode($json).')';
        die();
	}
   	public function getAction(){
		$model = $this->getReview();
		$request = $this->getRequest();
		$id = $request->getParam('assignment_id');
    	$callback = $request->getParam('callback');
        $r = $model->getByAssignmentId($id);
        $json = array();
		$reviews = array();
		$reviews[0] = $r->toArray();
        $json['totalCount'] = 1;
        $json['reviews'] = $reviews;
        echo $callback.'('.Zend_Json::encode($json).')';
        die();
	}
	public function getpaperAction(){
		$model = $this->getPaper();
		$request = $this->getRequest();
		$id = $request->getParam('paper_id');
    	$callback = $request->getParam('callback');
        $r = $model->getById($id);
        $json = array();
		$reviews = array();
		$reviews[0] = $r[0];
        $json['totalCount'] = 1;
        $json['papers'] = $reviews;
        echo $callback.'('.Zend_Json::encode($json).')';
        die();
	}
    protected function getReview()
    {
        if (null === $this->_model) {
            require_once APPLICATION_PATH . '/models/Review.php';
            $this->_model = new Model_Review();
        }
        return $this->_model;
    }
	protected function getPaper()
    {
        if (null === $this->_model) {
            require_once APPLICATION_PATH . '/models/Paper.php';
            $this->_model = new Model_Paper();
        }
        return $this->_model;
    }
}
